const { error } = require('console');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const readline = require('readline-sync');

//指定需要读取的表格根目录路径
let putDir = readline.question("dir:");


function getFileHash(algorithm, filePath) {
  return new Promise((resolve, reject) => {
    const hash = crypto.createHash(algorithm);
    const inputStream = fs.createReadStream(filePath);

    inputStream.on('error', reject);

    inputStream.on('data', (chunk) => {
      hash.update(chunk);
    });

    inputStream.on('end', () => {
      resolve(hash.digest('hex'));
    });
  });
}

async function getHash(filePath) {
  let hash = await getFileHash('sha256', filePath);
  return hash
}


const files = fs.readdirSync(putDir, "utf-8");
let haskArr = [];
files.forEach(file => {
  const filePath = path.join(putDir, file);
 
  const stat = fs.statSync(filePath);
  if (stat.isDirectory()) {
    // traverseFolder(filePath); // 递归遍历子文件夹
  } else {
    if (filePath.indexOf('.png') >= 0 && filePath.indexOf('$') == -1) {
      getHash(filePath).then((hash) => {
        if (haskArr.indexOf(hash) == -1) {
          haskArr.push(hash)
          console.log(hash)
        } else {
          fs.unlinkSync (filePath)
        }
      })
    }
  }
});

console.log(haskArr)


// areFilesEqual(file1, file2).then(areEqual => {
//   console.log(areEqual ? 'Files are equal.' : 'Files are not equal.');
// }).catch(error => {
//   console.error('Error:', error.message);
// });